package org.op.service.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.filechooser.FileSystemView;


public class PropertyFileConfigurationServiceImpl implements ConfigurationService {

	private static final String DEFAULT_WORKSPACE = "work";
	private String fileName;  // name of the configuration file
	private String filePath;  // path to the property file
	Properties props;
	
	public PropertyFileConfigurationServiceImpl() {
		props = new Properties();
	}
	
	public void init() throws IOException {
		
		// dont bother with this..
		
		/**
		ClassLoader loader = ClassLoader.getSystemClassLoader ();
		InputStream is = loader.getResourceAsStream(fileName);
		props.load(is);
		*/
		
		String applicationHome = getApplicationHome();
		
		
		File file = new File(applicationHome);
		if (!file.exists()) {
			file.mkdir();
		}
		
		filePath = applicationHome + FILE_SEP + fileName;
		
		// If workspace does not exist, create
		String workspaceProp = applicationHome + FILE_SEP + DEFAULT_WORKSPACE;
		File workspaceFolder = new File(workspaceProp);
		if (!workspaceFolder.exists()) {
			workspaceFolder.mkdir();
		}
		props.setProperty(ConfigKeys.KEY_CURRENT_WORKSPACE,workspaceProp);
		
		// load up the properties from the configuration file
		File configFile = new File(filePath);
		if (configFile.exists()) {
			InputStream instream = new FileInputStream(filePath);
			props.load(instream);
		}
		
		// initialize the log file path
		if (props.getProperty(ConfigKeys.APPLICATION_LOG_PATH)==null) {
			String appLogPath = applicationHome + FILE_SEP + props.getProperty(ConfigKeys.APP_NAME) + ".log";
			props.setProperty(ConfigKeys.APPLICATION_LOG_PATH, appLogPath);
		}
		
	}
	
	public Properties getApplicationProperties() {
		return props;
	}
	
	public String getProperty(String key) {
		return props.getProperty(key);
	}

	public void updateProperty(String propertyName, String value) throws Exception {
		
		props.setProperty(propertyName, value);
		
		String comment = "Updated property " + propertyName + " to " + value;
		saveProperites(comment);
	}

	private void saveProperites(String comment) throws Exception {
		
		File file = new File(filePath);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("Updating property file with " + comment);
		OutputStream out = new FileOutputStream(file);
		props.store(out, comment);
		
	}
	

	public String getApplicationHome() {
		
		// Use the My Documents/appName directory first
		// String rootPath = USER_HOME + FILE_SEP + "My Documents"; 
		// File file = new File(rootPath);
		String rootPath = System.getProperty("user.home");
		
		// this is the preferred approach
		File file = FileSystemView.getFileSystemView().getDefaultDirectory();
		if (file.exists()) {
			rootPath = file.getAbsolutePath();
		}
		
		String appHome = rootPath + FILE_SEP + props.getProperty(ConfigKeys.APP_NAME);		
		return appHome;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}


}
